import 'dart:ui' as ui show window;
import 'package:flutter/material.dart';
import 'package:flutter_swiper/flutter_swiper.dart';
import 'package:get/get.dart';
import 'package:ts_app_dart/routers/index.dart';
import 'package:ts_app_dart/widget/skip_down_time_progress.dart';

class SplashPage extends StatefulWidget {
  const SplashPage({Key key}) : super(key: key);

  @override
  _SplashPageState createState() => _SplashPageState();
}

class _SplashPageState extends State<SplashPage> {
  final List<String> _splashList = [
    'images/splash/app_start_1.webp',
    'images/splash/app_start_2.webp',
    'images/splash/app_start_3.webp'
  ];

  @override
  void initState() {
    super.initState();
  }

  void loadAdvertImgUrl() {
    String netImgUrl =
        'https://img2.baidu.com/it/u=1170834292,3580907519&fm=253&fmt=auto&app=138&f=JPG?w=500&h=889';
    // netImgUrl = '';
    // 模拟广告图接口请求
    Future.delayed(Duration(microseconds: 1000), () {
      // 有广告页：netImgUrl不为空
      if (netImgUrl.length > 0) {
        // 刷新页面使广告图显示
        setState(() {});
      } else {
        // 无广告页：netImgUrl 为空 - 跳转到主页
        Future.delayed(Duration(microseconds: 10), toIndexPage);
      }
    });
  }

  void toIndexPage() {
    // 浏览并删除所有以前的页面
    Get.offAllNamed(Routes.initial);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(children: [
        Swiper(
          itemCount: _splashList.length,
          itemBuilder: (_, index) {
            return Image.asset(
              _splashList[index],
              fit: BoxFit.cover,
            );

            // return FadeInImage(
            //   placeholder: AssetImage(_splashList[0]),
            //   fit: BoxFit.cover, // 适应图片的方式
            //   image: AssetImage(_splashList[index]),
            // );
          },
          onTap: (index) => toIndexPage(),
          loop: false,
          // autoplay: true,
          // 左右箭头
          // control: SwiperControl(),
          // 底部分页器
          //  pagination: SwiperPagination(),
        ),
        Offstage(
          offstage: true,
          child: Column(
            children: List.generate(
              _splashList.length,
              (index) => Container(
                child: Image.asset(_splashList[index]),
              ),
            ),
          ),
        ),
        Visibility(
          visible: false,
          // visible: _splashList.length > 0,
          child: Positioned(
              child: SkipDownTimeProgress(
                  color: Colors.red,
                  radius: 22.0,
                  duration: Duration(seconds: 5),
                  size: Size(25.0, 25.0),
                  skipText: "跳过",
                  onTap: () => toIndexPage(),
                  onFinishCallBack: (bool value) {
                    if (value) toIndexPage();
                  }),
              top: MediaQueryData.fromWindow(ui.window).padding.top + 20,
              right: 30),
        ),
      ]),
    );
  }
}
